from tkinter import *
import pygame
import pygame.mixer as pm
from pygame.locals import *
import random as r

main = Tk()
main.geometry('1280x720')

#背景音乐
pygame.init()
pm.init()
pm.music.load('斗地主.mp3')#加载音乐
pm.music.play(loops=-1)#播放音乐,播放次数0，1次，-1重复播放

#定义飞机类，属性，控制飞机移动
class Plane(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)   
        #加载飞机的图片
        self.image = pygame.image.load('img/img_plane.png')
        #飞机的初始位置
        self.rect = self.image.get_rect()#获取飞机图片的矩形区域
        self.rect.centerx = 640
        self.rect.bottom = 710
    #控制飞机移动11-13
    def update(self):
        #初始速度
        self.speedx = 0
        self.speedy = 0
        keys = pygame.key.get_pressed()#获取所有按键的列表
        if keys[pygame.K_RIGHT] == True:
            self.speedx += 10
        if keys[pygame.K_LEFT] == True:
            self.speedx -= 10
        self.rect.centerx += self.speedx#左右移动
        if keys[pygame.K_UP] == True:
            self.speedy += -10
        if keys[pygame.K_DOWN] == True:
            self.speedy += 10
        self.rect.centery += self.speedy  #上下移动
        #限制边界
        if self.rect.right > 1280:
            self.rect.right = 1280
        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.top < 0:
            self.rect.top = 0
        if self.rect.bottom > 720:
            self.rect.bottom = 720

#定义陨石类
class Yunshi(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('img/yunshi1.png')
        self.rect = self.image.get_rect()
        #设置出现位置
        self.rect.x = r.randint(0,1280-self.rect.width)
        self.rect.y = r.randint(-100,-30)
        #设置初始速度
        self.speedx = r.randint(1,3)
        self.speedy = r.randint(1,8)
    #11.27
    def update(self):
        self.rect.x += self.speedx
        self.rect.y += self.speedy
        if self.rect.top > 720 or self.rect.left < 0 or self.rect.right > 1300:
            self.rect.x = r.randint(0,1280-self.rect.width)
            self.rect.y = r.randint(-100,-30)

#定义子弹类
class Bullet(pygame.sprite.Sprite):
    def __init__(self,x,y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('img/img_bullet.png')
        self.rect = self.image.get_rect()
        #初始位置
        self.rect.y = y
        self.rect.centerx = x
        #初始速度
        self.speedy = -10
    def update(self):
        self.rect.y += self.speedy
        if self.rect.bottom < 0:
            self.kill()#超出屏幕删除自己


#开始游戏
def startGame():
    pm.music.stop()#停止播放
    screen = pygame.display.set_mode((1280,720))#设置屏幕大小
    #设置背景图片
    bg = pygame.image.load('img/img_bg.png')
    #帧数
    fps = 60
    clock = pygame.time.Clock()
    plane = Plane()#飞机对象
    all_sprites = pygame.sprite.Group()#创建角色组
    all_yunshi = pygame.sprite.Group()#创建陨石组
    all_bullet = pygame.sprite.Group()#创建子弹组
    all_sprites.add(plane)#加入角色组
    for i in range(8):
        yunshi = Yunshi()#陨石对象
        all_sprites.add(yunshi)
        all_yunshi.add(yunshi)
    running = True
    while running:
        screen.blit(bg, (0, 0))  # 重复显示背景
        clock.tick(fps)#11-20
        #循环获取飞机的位置
        bullet = Bullet(plane.rect.centerx,plane.rect.top)
        all_sprites.update()
        all_sprites.draw(screen)
        pygame.display.flip()  # 刷新界面
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running == False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    all_sprites.add(bullet)
                    all_bullet.add(bullet)#加入子弹组
                if event.key == pygame.K_ESCAPE:
                    running == False
        #单一角色和角色组碰撞
        hits = pygame.sprite.spritecollide(plane,all_yunshi,True)
        #角色组和角色组碰撞
        hits1 = pygame.sprite.groupcollide(all_yunshi,all_bullet,True,True)
        if hits:
            running = False
        #print(hits1)
        for i in hits1:
            yunshi = Yunshi()
            all_sprites.add(yunshi)
            all_yunshi.add(yunshi)
        

#背景图片
bgp = PhotoImage(file='ace.png')
bgps = Label(main, text="皇牌空战", justify=LEFT, image=bgp,compound=CENTER, font=('黑体', '40', 'bold', 'italic'), fg='gold')
bgps.pack()

#开始按钮
start = Button(main, text='开始游戏',bg='grey',font=('黑体', '15', 'bold', 'italic'), fg='gold',command=startGame)
start.pack()
start.place(x=1000,y=200,width=100,height=50)

#退出按钮
end = Button(main, text='退出游戏', bg='grey', font=('黑体', '15', 'bold', 'italic'), fg='gold')
end.pack()
end.place(x=1000, y=300, width=100, height=50)

main.mainloop()
# 彻底退出
pygame.quit()
